package com.example.fengyungo.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.fengyungo.R;
import com.example.fengyungo.activity.login.LoginActivity;
import com.example.fengyungo.activity.me.ChangePasswordActivity;
import com.example.fengyungo.activity.me.MyInfoActivity;
import com.example.fengyungo.activity.me.MyOrdersActivity;
import com.example.fengyungo.activity.me.VerifyActivity;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.TextConstant;
import com.example.fengyungo.domain.Driver;
import com.example.fengyungo.service.CommonIntentService;
import com.example.fengyungo.service.WebSocketService;
import com.example.fengyungo.utils.BitmapUtils;
import com.example.fengyungo.utils.SharedPreferencesUtils;
import com.google.android.material.card.MaterialCardView;

public class MeFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "MeFragment";

    /**
     * 我的订单
     */
    MaterialCardView myOrders;
    /**
     * 我的资料
     */
    MaterialCardView myInfo;
    /**
     * 修改密码
     */
    MaterialCardView changePasswordMc;
    /**
     * 司机认证
     */
    MaterialCardView driverVerify;
    /**
     * 司机昵称
     */
    TextView nicknameTextView;
    /**
     * 退出登录按钮
     */
    Button logoutButton;
    /**
     * 昵称
     */
    String nickname;
    /**
     * 已经登录过
     */
    boolean hasLogin;
    /**
     * 司机信息
     */
    Driver driver;
    /**
     * 头像
     */
    ImageView avatar;


    /**
     * 引入布局，获取控件的实例
     *
     * @param inflater           LayoutInflater
     * @param container          ViewGroup
     * @param savedInstanceState Bundle
     * @return View
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);

        nicknameTextView = view.findViewById(R.id.nickname);
        // 获取登录信息
        SharedPreferences preferences = getActivity().getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE);
        hasLogin = preferences.getBoolean(DriverConstant.CAN_LOGIN, false);
        // 获取司机信息
        driver = SharedPreferencesUtils.getDriverInfo(getActivity());
        nickname = driver.getNickname();
        // 设置司机的昵称到TextView
        nicknameTextView.setText(nickname);

        // 我的个人资料
        myInfo = view.findViewById(R.id.card_my_info);
        myInfo.setOnClickListener(this);

        // 我的订单
        myOrders = view.findViewById(R.id.my_orders_card);
        myOrders.setOnClickListener(this);

        // 修改密码
        changePasswordMc = view.findViewById(R.id.update_password_card);
        changePasswordMc.setOnClickListener(this);

        // 退出登录
        logoutButton = view.findViewById(R.id.logout);
        logoutButton.setOnClickListener(this);

        // 司机认证
        driverVerify = view.findViewById(R.id.driver_certification_card);
        driverVerify.setOnClickListener(this);

        // 头像
        avatar = view.findViewById(R.id.avatar);
        Bitmap bitmapAvatar = BitmapUtils.getBitmapFromSharedPreferences(getActivity(), DriverConstant.AVATAR);
        BitmapDrawable avatarDrawable = new BitmapDrawable(getResources(), bitmapAvatar);
        avatar.setBackground(avatarDrawable);

        return view;
    }

    /**
     * 点击对应按钮后的操作
     *
     * @param v View
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        // 我的订单
        if (id == R.id.my_orders_card) {
            Intent intent = new Intent(getActivity(), MyOrdersActivity.class);
            startActivity(intent);
        } else if (id == R.id.logout) {
            // 退出登录
            // 退出登录时清空SharedPreferences中司机的信息
            SharedPreferences.Editor editor = getActivity().getSharedPreferences(DriverConstant.SP_NAME, MODE_PRIVATE).edit();
            editor.clear();
            editor.apply();
            SharedPreferences.Editor editorImage = getActivity().getSharedPreferences(DriverConstant.SP_NAME_IMAGE, MODE_PRIVATE).edit();
            editorImage.clear();
            editorImage.apply();
            // 退出登录时停止服务
            Intent serviceIntent = new Intent(getActivity(), WebSocketService.class);
            getActivity().stopService(serviceIntent);
            Intent serviceIntent2 = new Intent(getActivity(), CommonIntentService.class);
            getActivity().stopService(serviceIntent2);

            getActivity().finish();
            // 跳转到登录页面
            // todo 未登录状态也可跳转登录
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else if (id == R.id.card_my_info) {
            // 我的详细信息
            if (hasLogin) {
                Intent intent = new Intent(getActivity(), MyInfoActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(getActivity(), TextConstant.NO_LOGIN, Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.driver_certification_card) {
            // 司机认证
            Intent intent = new Intent(getActivity(), VerifyActivity.class);
            startActivity(intent);
        } else if (id == R.id.update_password_card) {
            Intent intent = new Intent(getActivity(), ChangePasswordActivity.class);
            startActivity(intent);
        }
    }

}

